#include "mencoderprovider.h"
#include "encoderprovider.h"
#include "mencoderoption.h"
#include "h264settingwidget.h"

#include <QFileInfo>


static QString current_dir;

EncoderProvider * createProvider(const QString &path) {
    current_dir = path;
    return new MencoderProvider();
}

MencoderProvider::MencoderProvider()
{
    m_version.major = 1;
    m_version.minor = 1;
    m_version.other = 0;
    m_optionInc = 0;

    setup();
}

void MencoderProvider::setup() {
    m_audioCodecFormats << "mp3" << "ogg";
    m_videoCodecFormats << "h264" << "mpeg4";


    foreach (QString af, m_audioCodecFormats) {
	m_audioConfigWidgets[af] = *(new QHash<QString,QWidget *>());
    }

    foreach (QString af, m_videoCodecFormats) {
	m_videoConfigWidgets[af] = *(new QHash<QString,QWidget *>());
    }

}

QWidget * MencoderProvider::createSettingWidget(const QString & name) {
    QWidget *ret = NULL;
    if (name == "h264") {
	ret = new H264SettingWidget(NULL);
    }

    return ret;
}

const EncoderProviderVersion * MencoderProvider::version() const {
    return &m_version;
}

QString MencoderProvider::introduction() const {
    return tr("This's an introduction of mencoder");
}



void MencoderProvider::testAvailablityForFile(const QString &file, EncoderAvailablity &av) {
    QFileInfo finfo(file);
    if (!finfo.isFile() || !finfo.isReadable()) {
	av.audio_cap = false;
	av.subtitle_cap =  false;
	av.video_cap = false;
	return;
    }

    //TODO:Use mencoder to judge if this file has avaiblity to do things
    av.audio_cap = true;
    av.subtitle_cap = true;
    av.video_cap = true;
}

QStringList MencoderProvider::availableAudioCodecFormat() const {
   return m_audioCodecFormats;
}

QStringList MencoderProvider::availableVideoCodecFormat() const {
    return m_videoCodecFormats;
}

QWidget * MencoderProvider::configWidgetForAudioCodec(const QString & audio_fmt, const EncoderOption & op)  {
    if (!m_audioConfigWidgets.contains(audio_fmt)) return NULL;

    QHash<QString,QWidget *> hash = m_audioConfigWidgets[audio_fmt];

    return hash.value(op.id(),NULL);
}

QWidget * MencoderProvider::configWidgetForVideoCodec(const QString & video_fmt, const EncoderOption & op) {
    QWidget *ret = NULL;
    if (!m_videoConfigWidgets.contains(video_fmt)) return NULL;


    QHash<QString, QWidget *> sets = m_videoConfigWidgets[video_fmt];
    QVariant data;

    if (!sets.contains(op.id()))
    {
	ret = createSettingWidget(video_fmt);
	if (!ret) return ret;
	ret->setProperty("video_fmt",video_fmt);
	ret->setProperty("config_option",op.id());
	ret->setProperty("config_type","video");
	connect(ret,SIGNAL(destroyed(QObject*)),
		this,SLOT(onConfigWidgetDestroyed(QObject*)));
	if (!ret) return ret;

	sets[op.id()] = ret;
    }

    return sets[op.id()];
}

EncoderOption *MencoderProvider::createOption(EncoderOption *copy) {
    EncoderOption *option;
    if (copy){
	option = &copy->clone();
    }else{
	option = new MencoderOption();
    }

    QString id = QString::number(m_optionInc++);
    option->setId(id);

    return option;
}

bool MencoderProvider::startConvert(QStringList &) {
    return true;
}

void MencoderProvider::onConfigWidgetDestroyed(QObject *obj) {
    QWidget *widget = qobject_cast<QWidget *>(obj);
    if (!widget) return;

    QVariant data = widget->property("video_fmt");
    if (!data.isValid()) return;

    QString video_fmt, config_option,config_type;
    video_fmt = data.toString();
    config_option = widget->property("config_option").toString();
    config_type = widget->property("config_type").toString();

    if (config_type == "video"){
	if (!m_videoConfigWidgets.contains(video_fmt)) return;
	if (!m_videoConfigWidgets[video_fmt].contains(config_option)) return;

	m_videoConfigWidgets[video_fmt].remove(config_option);
    }else if (config_type == "audio"){
	if (!m_audioConfigWidgets.contains(video_fmt)) return;
	if (!m_audioConfigWidgets[video_fmt].contains(config_option)) return;

	m_audioConfigWidgets[video_fmt].remove(config_option);
    }
}
